package cn.com.manager.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class SetCharacterEncodingFilter implements Filter {

	private FilterConfig filterConfig;
	private String encodingName;
	private boolean enable;

	public SetCharacterEncodingFilter() {
		this.encodingName = "UTF-8";
		this.enable = false;
	}

	@Override
	public void destroy() {

	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain filterChain)
			throws IOException, ServletException {
		if (this.enable) {
			arg0.setCharacterEncoding(this.encodingName);
		}
		filterChain.doFilter(arg0, arg1);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		this.filterConfig = arg0;
		this.encodingName = this.filterConfig.getInitParameter("encodingName");
		this.enable = Boolean.parseBoolean(this.filterConfig.getInitParameter("enable"));
	}

}
